﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Sklad1_2.Add.Update;
using Sklad1_2.Data;
using Sklad1_2.Default;

namespace Sklad1_2.Search
{
    public partial class SearchProduct : Defaultform
    {
        public SearchProduct()
        {
            InitializeComponent();
            Init();
        }

        private string query;
        public override void Init()
        {
            ABase.AddItem(comboBox1,"angar");
            ABase.AddItemDBGridView(dataGridView1,Helper.QueryProduct);
            Helper.Hidden(dataGridView1,0,1,2,3);
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            comboBox1.Enabled = radioButton1.Checked;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            button2.Enabled=textBox1.Enabled = radioButton2.Checked;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            query = Helper.QueryProduct;
            Type t = typeof (Int32);
            if (comboBox1.SelectedValue!=null)
            {
                if (comboBox1.SelectedValue.GetType() == t)
                {
                    query += " and d.sklad_id=" + Convert.ToString(comboBox1.SelectedValue);
                }
                ABase.AddItemDBGridView(dataGridView1,query);
                Helper.Hidden(dataGridView1, 0, 1, 2, 3);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            query = Helper.QueryProduct;
            query += " and d.product_name like '%"+textBox1.Text+"%'";
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            data=new List<string>();
            Helper.Db2List(dataGridView1,e,ref data,3,0,1,2,7,8,9);
            UpdateProduct aProduct=new UpdateProduct(data);
            aProduct.FormClosed += Bind;
            aProduct.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                query = Helper.QueryProduct;
                query += " and d.product_name like '%" + textBox1.Text + "%'";
                ABase.AddItemDBGridView(dataGridView1,query);
                Helper.Hidden(dataGridView1, 0, 1, 2, 3);
            }
        }

    }
}
